<?php

/**
 * This is the model class for table "cad_turma".
 *
 * The followings are the available columns in table 'cad_turma':
 * @property integer $cod_turma
 * @property integer $cod_periodo
 * @property string $nome
 * @property string $carga_hora
 * @property integer $numero_alunos
 * @property string $professor
 */
class cad_turma extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return cad_turma the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cad_turma';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cod_periodo,cod_curso, nome, carga_hora, numero_alunos, professor', 'required'),
			array('cod_periodo, cod_curso,numero_alunos', 'numerical', 'integerOnly'=>true),
			array('nome, carga_hora, professor', 'length', 'max'=>150),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cod_turma, cod_periodo, nome, carga_hora, numero_alunos, professor', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cad_alunos' => array(self::HAS_MANY, 'CadAluno', 'cod_turma'),
			'cod_periodo0' => array(self::BELONGS_TO, 'CadPeriodo', 'cod_periodo'),
                        'cod_curso0' => array(self::BELONGS_TO, 'CadCurso', 'cod_curso'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cod_turma' => 'Turma',
			'cod_periodo' => 'Periodo',
                        'cod_curso' => 'Curso',
			'nome' => 'Nome',
			'carga_hora' => 'Carga Horária',
			'numero_alunos' => 'Número de Alunos',
			'professor' => 'Professor',
		);
	}


	public static function loadTableTurma($id=null)
	{
	    if(!isset($id)){
                $lista  = array();
                $models = Cad_turma::model()->findAll();
                foreach($models as $model){
			//$lista[$model->cad_associacao]['id']   = $model->$model->cad_associacao;
                        $lista[$model->cod_turma] = $model->nome;
                }
               return $lista;
            }else{
                   $models = Cad_turma::model()->findAll(array('condition'=>'cod_turma=:x', 'params'=>array(':x'=>$id)));
                foreach($models as $model)
                  return $model->nome;
            }
	}
        

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cod_turma',$this->cod_turma);

		$criteria->compare('cod_periodo',$this->cod_periodo);

                $criteria->compare('cod_curso',$this->cod_curso);

		$criteria->compare('nome',$this->nome,true);

		$criteria->compare('carga_hora',$this->carga_hora,true);

		$criteria->compare('numero_alunos',$this->numero_alunos);

		$criteria->compare('professor',$this->professor,true);

		return new CActiveDataProvider('cad_turma', array(
			'criteria'=>$criteria,
		));
	}
}